package com.ykdl.member.kid.circle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import oauth.signpost.http.HttpParameters;
import android.R.integer;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.kid.R;
import com.ykdl.member.KidApp;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.adapters.PopwindowAdapter;
import com.ykdl.member.kid.beans.Listinfobean;
import com.ykdl.member.kid.beans.OptionsBean;
import com.ykdl.member.kid.beans.ResetPasswordBean;
import com.ykdl.member.kid.beans.TopicBean;
import com.ykdl.member.kid.beans.TopicListBean;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.image.ImageLoader;
import com.ykdl.member.kid.image.ImageOptions;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.person.PersonCenterActivity;
import com.ykdl.member.kid.regandlogin.RegAndLoginActivity;
import com.ykdl.member.kid.util.DateUtil;
import com.ykdl.member.kid.util.MobileUtils;
import com.ykdl.member.kid.widget.AsyncImageView;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.member.kid.widget.RefreshListView.OnRefreshListener;
import com.ykdl.member.views.MyGridView;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class CircleTopicActivity extends BaseScreen implements OnClickListener{
	private AsyncImageView circle_head;
	private TextView group_name,membernum,contentnum ,sort_new ,
	sort_laste_result ,sort_the_sanme_age, sort_picture,main_title;
	private Button signin_but ,isinput_but ,title_xiala;
	private String group_id  = "" ;
	private String TAG = "CircleTopicActivity";
	private Listinfobean groupbean ;
	private ArrayList<TopicBean>  topic_list = new ArrayList<TopicBean>();
	private RefreshListView topic_listview ;
	private CircleTopicAdapter adapter ;
	private LinearLayout title_popbk ;
	private GridView topic_picture_gridview ;
	private TopicPictureAdapter pictureAdapter ;
	ImageOptions options ;
	private String sum_count ;
	private String group_namestring;
	private PopupWindow m_popupWindow;
	private ListView mtListView;
	private ArrayList<OptionsBean> optionslist ;
	private int sort = -1 ;
	private int condition = -90 ;
	private boolean islook_picture= false;
	private boolean issetleftlist= false;
	private int start_id = 0;
	private int cursor = 0;
	private int count = 20;
	private BitmapUtils bitmapUtil ;
	private BitmapUtils bitmapUtil_pic ;
	private int gradview_item_image_height,gradview_width,gradview_item_width,screenWidth;
	private boolean isloaddate=false;
	private View header = null;
	private KidApp mApplication ;
	private Drawable left ;
	private int start_int = 1;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.circletopicactivity_xml);
		bitmapUtil = new BitmapUtils(this);
		bitmapUtil.configDefaultLoadingImage(R.drawable.defualt_icon);
		bitmapUtil.configDefaultLoadFailedImage(R.drawable.defualt_icon);
		bitmapUtil_pic= new BitmapUtils(this);
		bitmapUtil_pic.configDefaultLoadingImage(R.drawable.iask_main_middle_img);
		bitmapUtil_pic.configDefaultLoadFailedImage(R.drawable.iask_main_middle_img);
		mApplication=(KidApp) this.getApplication();
		left = this.getResources().getDrawable(R.drawable.iask_newlist_zhiding);
		left.setBounds(0, 0, left.getIntrinsicWidth(), left.getIntrinsicHeight());
		inint();
	}
	public void inint() {
		mtListView = new ListView(this);
		header = LayoutInflater.from(mContext).inflate(
				R.layout.circletopicactivity_header_xml, null);
		circle_head = (AsyncImageView)header.findViewById(R.id.head);
		group_name = (TextView)header.findViewById(R.id.group_name);
		contentnum = (TextView)header.findViewById(R.id.content_num);
		membernum = (TextView)header.findViewById(R.id.member_num);
		signin_but = (Button)header.findViewById(R.id.signin_but);
		isinput_but = (Button)header.findViewById(R.id.isinput_but);
		main_title = (TextView)findViewById(R.id.main_title);
		title_popbk= (LinearLayout)findViewById(R.id.title_popbk);
		
		sort_new = (TextView)findViewById(R.id.sort_new);
		sort_laste_result = (TextView)findViewById(R.id.sort_laste_result);
		sort_the_sanme_age = (TextView)findViewById(R.id.sort_the_sanme_age);
		sort_picture = (TextView)findViewById(R.id.sort_picture);
		title_xiala = (Button)findViewById(R.id.title_xiala);
		title_xiala.setOnClickListener(this);
		sort_new.setOnClickListener(this);
		sort_laste_result.setOnClickListener(this);
		sort_the_sanme_age.setOnClickListener(this);
		sort_picture.setOnClickListener(this);
		findViewById(R.id.main_right).setOnClickListener(this);
		findViewById(R.id.main_left).setOnClickListener(this);
		topic_picture_gridview = (GridView)findViewById(R.id.topic_picture_gridview);
		inintGridview();
		topic_listview = (RefreshListView)findViewById(R.id.topic_listview);
		topic_listview.addHeaderView(header);
		topic_picture_gridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				TopicBean bean = (TopicBean)pictureAdapter.getItem(arg2);
				Intent intent = new Intent(mContext, TopicAndResultActivity.class);
				intent.putExtra(TopicAndResultActivity.STR_TOPIC_ID, bean.getTopic_id());
				intent.putExtra(TopicAndResultActivity.STR_GROUP_ID, group_id);
				startActivity(intent);
			}
			
		});
		topic_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				TopicBean bean = (TopicBean)adapter.getItem(arg2-1);
				Intent intent = new Intent(mContext, TopicAndResultActivity.class);
				intent.putExtra(TopicAndResultActivity.STR_TOPIC_ID, bean.getTopic_id());
				intent.putExtra(TopicAndResultActivity.STR_GROUP_ID, group_id);
				startActivity(intent);
			}
			
		});
		topic_listview.setMoreClick(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				issetleftlist = true;
				++start_int ;
				count =20 ;
				getGroupData();
			}
		});
		topic_listview.setonRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub
				issetleftlist = false;
				topic_list = new ArrayList<TopicBean>();
				count = 20 ;
				cursor = 0;
				getGroupData();
			}});
		signin_but.setOnClickListener(this);
		isinput_but.setOnClickListener(this);
		options = ImageOptionsFactory.getSmallPortraitOptions(CircleTopicActivity.this);
		if (getIntent()!= null) {
			group_id = getIntent().getStringExtra("group_id");
		}
		adapter = new CircleTopicAdapter();
		pictureAdapter  = new TopicPictureAdapter();
		getGroupData();
		
	}
	public void inintGridview() {
		 screenWidth = MobileUtils.getScreenWidthIntPx();
		 gradview_width = (screenWidth - MobileUtils.dpToPx(12));
		 gradview_item_width = gradview_width / 3; 
		 gradview_item_image_height = (int)((gradview_item_width - MobileUtils.dpToPx(12)) / 0.97); //计算每个item的高度
		
		LayoutParams  params = (LayoutParams) topic_picture_gridview.getLayoutParams();
		params.width = gradview_width;
		params.rightMargin = MobileUtils.dpToPx(6);
		params.leftMargin = MobileUtils.dpToPx(6);
		
		topic_picture_gridview.setHorizontalSpacing(MobileUtils.dpToPx(12));
		topic_picture_gridview.setVerticalSpacing(MobileUtils.dpToPx(12));
		topic_picture_gridview.setColumnWidth(gradview_item_width);
		topic_picture_gridview.setCacheColorHint(getResources().getColor(R.color.touming));
		topic_picture_gridview.setNumColumns(3);
	}
	public void setAdapter(ArrayList<OptionsBean> optionslist) {
		final PopwindowAdapter lpopwindowAdapter = new PopwindowAdapter(this, optionslist);
		mtListView.setAdapter(lpopwindowAdapter);
		mtListView.setSelector(getResources().getDrawable(R.drawable.title_popsel));
		mtListView.setCacheColorHint(0);
		mtListView.setDivider(null);
		mtListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				OptionsBean bean = (OptionsBean)lpopwindowAdapter.getItem(arg2);
				main_title.setText(bean.getName());
				if (bean.getName().endsWith("查看图片")) {
					islook_picture = true ;
				}else {
					islook_picture = false ;
				}
				sort = bean.getParams().getSort();
				condition = bean.getParams().getCondition();
				count = 20;
				m_popupWindow.dismiss();
				getGroupData();
			}
			
		});
	}
	public void initPopupWindow(View view,ListView listview) {
		int popwidth =(int)this.getResources().getDimension(R.dimen.top_pop_width);
		int pophight =(int)this.getResources().getDimension(R.dimen.top_pop_hight);
		m_popupWindow = new PopupWindow(listview, popwidth, pophight,true);
		m_popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.title_popbk));
		WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);  
		int xPos = (int) ((-windowManager.getDefaultDisplay().getWidth() /2)+m_popupWindow.getWidth()/2) ; 
		
		m_popupWindow.showAsDropDown(view,xPos,0);
		m_popupWindow.setOnDismissListener(new OnDismissListener() {
			
			@Override
			public void onDismiss() {
				// TODO Auto-generated method stub
				title_xiala.setSelected(false);
			}
		});
	}
	public void getGroupData() {
		if (!issetleftlist) {
			showProgress("正在加载,请稍等...");
		}
//		String  pageurl = "http://172.16.22.52:5000/api/v1/groups/30/topics";
		String  pageurl = KidConfig.GET_ALL_GROUPS+"/"+group_id+"/"+"topics";
		Map<String,String> map = new HashMap<String, String>();
		if (sort!=-1) {
			map.put("sort", sort+"");
		}
		if (condition!=-90) {
			map.put("condition", condition+"");
		}
		map.put("start_id", String.valueOf(start_id));
		map.put("cursor", String.valueOf(cursor));
		map.put("count", String.valueOf(count));
		HttpBaseRequest request  =  Wxxr.getInstance().getRequest(pageurl, map,null);
		 TaskManager.startHttpRequest(request, new gettopiclistTag(request),  TopicListBean.class);
	}
	class gettopiclistTag implements ITag {
		HttpBaseRequest request ;
		public gettopiclistTag(HttpBaseRequest request){
			this.request = request ;
		}
		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof TopicListBean) {
				TopicListBean bean = (TopicListBean)t;
				int index = 0;
				if (topic_list.size() > 1) {
					index = topic_list.size() - 1;
				}
				for (TopicBean obj : bean.getList()) {
					topic_list.add(obj);
				}
//				topic_list = bean.getList();
				if (islook_picture) {
					topic_listview.setVisibility(View.GONE);
					topic_picture_gridview.setVisibility(View.VISIBLE);
					pictureAdapter.setList(topic_list);
					pictureAdapter.notifyDataSetChanged();
					topic_picture_gridview.setAdapter(pictureAdapter);
				}else {
					topic_listview.setVisibility(View.VISIBLE);
					topic_picture_gridview.setVisibility(View.GONE);
					adapter.setList(topic_list);
					adapter.notifyDataSetChanged();
					if (bean.getNext_cursor() == bean.getTotal_number()) {
						topic_listview.setMoreButtoIsGon(true);
					} else {
						cursor = bean.getNext_cursor();
						topic_listview.setMoreButtoIsGon(false);
					}
					groupbean = bean.getGroup();
					optionslist = bean.getGroup().getTopic_options();
					setAdapter(optionslist);
					setData(groupbean);
					topic_listview.setAdapter(adapter);
					if (issetleftlist) {
						topic_listview.setSelection(index);
					}
					topic_listview.onRefreshComplete();
					topic_listview.setFecthMoreOk();
				}
				}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			return true;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			request.cancel();
		}
		
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().setCurrentActivity(TAG);
		super.onResume();
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().pause(TAG);
		super.onPause();
	}
	public void setData(Listinfobean bean) {
		if (bean.getIcon_file_meta()!=null) {
			circle_head.setUrl(TAG, bean.getIcon_file_meta().getDownload_urls().getBig().getUrl(),
					ImageOptionsFactory.getSmallPortraitOptions(CircleTopicActivity.this));
		}
		circle_head.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent  = new Intent(mContext, CircleInfoActivity.class);
				intent.putExtra("group_id", group_id);
				startActivity(intent);
			}
		});
		group_namestring = bean.getGroup_name();
		group_name.setText(bean.getGroup_name());
		contentnum.setText("话题:"+bean.getTopic_count());
		membernum.setText("成员:"+bean.getMember_count());
		signin_but.setSelected(bean.isIs_registration());
		isinput_but.setSelected(bean.isIs_member());
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.signin_but:
			if (mApplication.mMyUser.iAnonymousToken()) {
				Intent intent=new Intent(CircleTopicActivity.this,RegAndLoginActivity.class);
				intent.putExtra(KidAction.FROM, "");
				startActivity(intent);
			}else {
				if (signin_but.isSelected()) {
					Toast.makeText(mContext, "已签到", 1).show();
				}else {
					signinGroup(group_id);
				}
			}
			break;
		case R.id.isinput_but:
			if (mApplication.mMyUser.iAnonymousToken()) {
				Intent intent=new Intent(CircleTopicActivity.this,RegAndLoginActivity.class);
				intent.putExtra(KidAction.FROM, "");
				startActivity(intent);
			}else {
				if (isinput_but.isSelected()) {
					deltGroup(group_id);
				}else {
					joinGroup(group_id);
				}
			}
			
			break;
		case R.id.title_xiala:
			if (title_xiala.isSelected()) {
				title_xiala.setSelected(false);
			} else {
				title_xiala.setSelected(true);
				initPopupWindow(title_xiala,mtListView);
			}
			
			break;
		case R.id.sort_new:
			setSelect(R.id.sort_new);
			break;
		case R.id.sort_laste_result:
			setSelect(R.id.sort_laste_result);
			break;
		case R.id.sort_the_sanme_age:
			setSelect(R.id.sort_the_sanme_age);
			break;
		case R.id.sort_picture:
			setSelect(R.id.sort_picture);
			break;
		case R.id.main_left:
			Intent intent =null;
			if (mApplication.mMyUser.iAnonymousToken()) {
				intent=new Intent(CircleTopicActivity.this,RegAndLoginActivity.class);
				intent.putExtra(KidAction.FROM, "");
				startActivity(intent);
			}else {
				 intent = new Intent(mContext, NewTopicActivity.class);
				intent.putExtra("group_id", group_id);
				startActivityForResult(intent, 0);
			}
			
			break;
		case R.id.main_right:
			if (isloaddate) {
				setResult(RESULT_OK);
			}
			this.finish();
			break;
			
		default:
			break;
		}
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (resultCode==RESULT_OK) {
			start_id = 0;
			cursor = 0;
			count = 20;
			getGroupData();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	public void deltGroup(String group_id ) {
		showProgress("正在加载,请稍等...");
		String  pageurl = KidConfig.GET_GROUP_MEMBER;
		HttpParameters bodyParams = new HttpParameters();
//		bodyParams.put("", value)
		HttpBaseRequest request  =  Wxxr.getInstance().deltteRequest(pageurl+group_id,bodyParams);
		 TaskManager.startHttpRequest(request, new deltgroupTag(group_id,request),  ResetPasswordBean.class);
	}
	private class deltgroupTag implements ITag {
		private String id ;
		HttpBaseRequest request ;
		public deltgroupTag(String group_id,HttpBaseRequest request){
			id = group_id ;
			this.request = request ;
		}
		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof ResetPasswordBean) {
				ResetPasswordBean bean = (ResetPasswordBean)t;
				if (bean.getStatus()==0) {
					isinput_but.setSelected(false);
					isloaddate = true ;
					Toast.makeText(mContext, "退出成功", 1).show();
				}
				
			}
			
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			Toast.makeText(mContext, "退出失败", 1).show();
			finishProgress();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			request.cancel();
		}
		
	}
	public void joinGroup(String group_id ) {
		showProgress("正在加载,请稍等...");
		String  pageurl = KidConfig.GET_GROUP_MEMBER;
		HttpParameters bodyParams = new HttpParameters();
		HttpBaseRequest request  =  Wxxr.getInstance().postBaseRequest(pageurl+group_id,bodyParams, null);
		 TaskManager.startHttpRequest(request, new joingroupTag(group_id,request),  ResetPasswordBean.class);
	}
	private class joingroupTag implements ITag {
		private String id ;
		HttpBaseRequest request ;
		public joingroupTag(String group_id,HttpBaseRequest request){
			id = group_id ;
			this.request = request ;
		}
		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof ResetPasswordBean) {
				ResetPasswordBean bean = (ResetPasswordBean)t;
				if (bean.getStatus()==0) {
					isinput_but.setSelected(true);
					isloaddate = true ;
					Toast.makeText(mContext, "加入成功", 1).show();
				}
				
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加入失败", 1).show();
			return true;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			request.cancel();
		}
		
	}
	public void signinGroup(String group_id ) {
		showProgress("正在加载,请稍等...");
		String  pageurl = KidConfig.GROUPS_SIGNIN;
		HttpBaseRequest request  =  Wxxr.getInstance().getBaseRequest(pageurl+group_id);
		 TaskManager.startHttpRequest(request, new signinroupTag(group_id,request),  ResetPasswordBean.class);
	}
	private class signinroupTag implements ITag {
		private String id ;
		HttpBaseRequest request ;
		public signinroupTag(String group_id,HttpBaseRequest request){
			id = group_id ;
			this.request = request ;
		}
		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof ResetPasswordBean) {
				ResetPasswordBean bean = (ResetPasswordBean)t;
				if (bean.getError()==null) {
					signin_but.setSelected(true);
					if (bean.getStatus()==0) {
						Toast.makeText(mContext, "签到成功，经验值+2", 1).show();
					}else {
						Toast.makeText(mContext, "签到成功", 1).show();
					}
					isloaddate = true ;
				}else {
					Toast.makeText(mContext, "签到失败,"+bean.getDesc(), 1).show();
					isloaddate = false ;
				}
				
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			Toast.makeText(mContext, "签到失败", 1).show();
			isloaddate = false ;
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			request.cancel();
		}
		
	}
	class TopicPictureAdapter extends BaseAdapter {
		
		ArrayList<TopicBean>   list;

		public void setList(ArrayList<TopicBean> list) {
			this.list = list;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			if (list==null) {
				return 0;
			}else {
				return list.size();
			}
		
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = RelativeLayout.inflate(mContext,
						R.layout.picture_gridview_item_xml, null);
				holder = new ViewHolder();
				holder.head  = (ImageView) convertView.findViewById(R.id.topic_picture);
				holder.topic_pictur_lp = (LinearLayout) convertView.findViewById(R.id.picture_bg);
//				LayoutParams   iv_heat_image_params = 	(LayoutParams) holder.head.getLayoutParams();
//				iv_heat_image_params.width = LayoutParams.FILL_PARENT;
//				iv_heat_image_params.height = gradview_item_image_height;
//				holder.head.setLayoutParams(iv_heat_image_params);
				convertView.setTag(holder);
			}else {
				holder = (ViewHolder) convertView.getTag();
			}
			
			if (list.get(position).getFile_metas().size()==2) {
				holder.topic_pictur_lp.setBackgroundResource(R.drawable.topic_pictur_lp1);
			}else if (list.get(position).getFile_metas().size()>2){
				holder.topic_pictur_lp.setBackgroundResource(R.drawable.topic_pictur_lp2);
			}else {
				holder.topic_pictur_lp.setBackgroundDrawable(null);
			}
			String url = list.get(position).getFile_metas().get(0).getDownload_urls().getMiddle().getUrl();
			bitmapUtil_pic.display(holder.head, url);
			return convertView;
		}
		
	}
	class CircleTopicAdapter extends BaseAdapter {
		ArrayList<TopicBean>   list;

		public void setList(ArrayList<TopicBean> list) {
			this.list = list;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			if (list==null) {
				return 0;
			}else {
				return list.size();
			}
		
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			 return list.get(arg0-1);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = RelativeLayout.inflate(mContext,
						R.layout.topic_item, null);
						holder = new ViewHolder();
						holder.head = (ImageView) convertView.findViewById(R.id.user_head);
						holder.grade = (TextView) convertView.findViewById(R.id.grade);
						holder.name = (TextView) convertView.findViewById(R.id.name);
						holder.group_name = (TextView) convertView.findViewById(R.id.group_name);
						holder.reply_count = (TextView) convertView.findViewById(R.id.reply_count);
						holder.conent = (TextView) convertView.findViewById(R.id.conent);
						holder.date = (TextView) convertView.findViewById(R.id.iask_item_time);
						holder.is_digest = (ImageView) convertView.findViewById(R.id.is_digest);
						holder.iask_zhiding = (ImageView) convertView.findViewById(R.id.iask_zhiding);
						holder.topic_pictur = (ImageView) convertView.findViewById(R.id.topic_pictur_lp); 
						convertView.setTag(holder);
			}else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.date.setText(DateUtil.gettopicDate(list.get(position).getPost_time()));
			if (list.get(position).getPost_actor()!=null) {
				holder.name.setText(list.get(position).getPost_actor().getDisplay_name());
				if (list.get(position).getIs_anonymous().equals("0")) {
					holder.grade.setText(list.get(position).getPost_actor().getLevel()+"");
					if (list.get(position).getPost_actor().getAvatar_file_meta()!=null) {
						String url = list.get(position).getPost_actor().getAvatar_file_meta().getDownload_urls().getSmal().getUrl();
						bitmapUtil.display(holder.head, url);
					}else {
						holder.head.setImageResource(R.drawable.defualt_icon);
					}
					final TopicBean bean = list.get(position) ;
					holder.head.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							
								go(PersonCenterActivity.class, PersonCenterActivity.STR_ACTOR_ID, bean.getPost_actor().getActor_id());
							
							
						}
					});
				}else {
					holder.name.setText("匿名用户");
					holder.head.setImageResource(R.drawable.defualt_icon);
				}
				
			}
			holder.group_name.setText(groupbean.getGroup_name());
			holder.reply_count.setText(list.get(position).getReply_count()+"");
			sum_count = list.get(position).getReply_count() ;
			
			if (list.get(position).getDigest_level()>0) {
				holder.is_digest.setVisibility(View.VISIBLE);
			}else {
				holder.is_digest.setVisibility(View.GONE);
			}
			if (list.get(position).getDisplay_order()>0) {
//			holder.iask_zhiding.setVisibility(View.VISIBLE);
				SpannableString spannable = new SpannableString("1234"+list.get(position).getTitle());
				ImageSpan span = new ImageSpan(left, ImageSpan.ALIGN_BASELINE);
				spannable.setSpan(span, 0, 4,  Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
			holder.conent.setText(spannable);
			}else {
				holder.iask_zhiding.setVisibility(View.GONE);
//				holder.conent.setCompoundDrawables(null, null, null, null);
				holder.conent.setText(list.get(position).getTitle());
			}
			if (list.get(position).isHas_image()) {
				holder.topic_pictur.setVisibility(View.VISIBLE);
				String url = list.get(position).getFile_metas().get(0).getDownload_urls().getBig().getUrl();
				bitmapUtil_pic.display(holder.topic_pictur, url);
			}else {
				holder.topic_pictur.setVisibility(View.GONE);
			}
			
			
			return convertView;
		}
		
	}
	class ViewHolder {
		ImageView head;
		TextView name;
		TextView grade;
		TextView group_name;
		TextView reply_count;
		TextView conent;
		TextView date;
		ImageView is_digest;
		ImageView iask_zhiding ;
		ImageView topic_pictur;
		LinearLayout topic_pictur_lp ;
	}
	private void setSelect(int id) {
		sort_new.setBackgroundResource(0);
		sort_laste_result.setBackgroundResource(0);
		sort_the_sanme_age.setBackgroundResource(0);
		sort_picture.setBackgroundResource(0);
		if (id == R.id.sort_new) {
			sort = 1;
			topic_picture_gridview.setVisibility(View.GONE);
			topic_listview.setVisibility(View.VISIBLE);
			sort_new.setBackgroundResource(R.drawable.title_popsel);
			main_title.setText(sort_new.getText());
			getGroupData();
		} else if (id == R.id.sort_laste_result) {
			sort = 2;
			topic_picture_gridview.setVisibility(View.GONE);
			topic_listview.setVisibility(View.VISIBLE);
			sort_laste_result.setBackgroundResource(R.drawable.title_popsel);
			main_title.setText(sort_laste_result.getText());
			getGroupData();
		} else if (id == R.id.sort_the_sanme_age) {
			sort = 3;
			topic_picture_gridview.setVisibility(View.GONE);
			topic_listview.setVisibility(View.VISIBLE);
			sort_the_sanme_age.setBackgroundResource(R.drawable.title_popsel);
			main_title.setText(sort_the_sanme_age.getText());
			getGroupData();
		} else if (id == R.id.sort_picture) {
			sort = 4;
			topic_listview.setVisibility(View.GONE);
			topic_picture_gridview.setVisibility(View.VISIBLE);
			sort_picture.setBackgroundResource(R.drawable.title_popsel);
			main_title.setText(sort_picture.getText());
			getGroupData();
		}
		title_xiala.setSelected(false);
		title_popbk.setVisibility(View.GONE);
	}
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		if (isloaddate) {
			setResult(RESULT_OK);
		}
		this.finish();
		super.onBackPressed();
	}
}
